// pages/my/familyInvite.js
const app = getApp();
var topOpacity = 0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 组件参数设置，传递到组件
    defaultData: {
      title: "家人邀请", // 导航栏标题
      showHome: false, //是否显示回到首页按钮
      showBack: true, //是否显示返回按钮
      showLogo: false, //是否显示logo
      bgColor: 'transparent'
    },
    baseUrl: app.baseUrl,
    version: app.globalData.version,
    navBarHeight: app.globalData.navBarHeight,
    cardNum: '',

    openid: '',
    shareTitle: '',
    shareImg: '',
    familyId: 0,
    billId: '',
    is_register: 0,
    shareOpenid: '',
    tel: '',
    nickname: '',
    head: '',
    tipsContent: ''
  },
  onPageScroll(t) {
    if (t.scrollTop < 40) {
      topOpacity = 0
      if (this.data.topOpacity != 0) {
        this.setData({
          topOpacity: 0
        })
      }
    } else if (t.scrollTop >= 40) {
      if (topOpacity <= 1) {
        topOpacity = t.scrollTop / 100 - .4
      } else {
        topOpacity = 1
      }
      if (topOpacity <= 1) {
        this.setData({
          topOpacity: topOpacity
        })
      }
    }
  },

  // 身份证号
  cardNumIpt(e) {
    this.setData({
      cardNum: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this;
    var familyId = options.familyId;
    var billId = options.billId;
    //billId = '92508023714a4860abf8eb5e93e749c2';
    //familyId = '884737';

    var shareOpenid = options.shareOpenid;
    //shareOpenid = 'oZYOb4ktAvmrb5TsIPBHlrGEKcZg';

    if (familyId == undefined || familyId == 0 || familyId == '') {
      app.showTip('抱歉,未获取到家庭id哦');
      setTimeout(function() {
        wx.switchTab({
          url: '/pages/index/index'
        });
      }, 1500);
      return;
    }
    if (billId == undefined || billId == 0 || billId == '') {
      app.showTip('抱歉,未获取到邀请id哦');
      setTimeout(function() {
        wx.switchTab({
          url: '/pages/index/index'
        });
      }, 1500);
      return;
    }

    that.setData({
      familyId: familyId,
      billId: billId,
      shareOpenid: shareOpenid
    });

    //获取缓存中的openid
    var openid = wx.getStorageSync('ly24_openid');

    if (openid == '') {
      //缓存中没有openid
      //获取所需页面信息
      wx.login({
        success: function(res) {
          if (res.code) {
            //发起网络请求
            wx.request({
              url: that.data.baseUrl + 'getOpenid.php',
              method: 'POST',
              data: {
                'code': res.code
              },
              header: {
                'content-type': 'application/x-www-form-urlencoded'
              },
              success: function(res) {
                //缓存微信openid
                wx.setStorageSync('ly24_openid', res.data.openid);
                that.setData({
                  openid: res.data.openid
                });

                if (shareOpenid == res.data.openid) {
                  wx.redirectTo({
                    url: '/pages/my/familyManage'
                  });
                  return;
                }

                //获取本页数据
                that.getMyFamilyInvitePageInfo(res.data.openid, familyId, shareOpenid, billId);
              },
              fail: function(res) {}
            });
          }
        }
      });
    } else {
      if (shareOpenid == openid) {
        wx.redirectTo({
          url: '/pages/my/familyManage'
        });
        return;
      }

      that.setData({
        openid: openid
      });

      //获取本页数据
      that.getMyFamilyInvitePageInfo(openid, familyId, shareOpenid, billId);
    }

    //调用分享
    that.getShareInfo();
  },

  //获取本业数据
  getMyFamilyInvitePageInfo(openid, familyId, shareOpenid, billId) {
    var that = this;
    wx.showLoading({
      title: '数据加载中...'
    })
    wx.request({
      url: that.data.baseUrl + 'getMyFamilyInvitePageInfo.php',
      method: 'POST',
      data: {
        'openid': openid,
        'familyId': familyId,
        'shareOpenid': shareOpenid,
        'billId': billId
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function(res) {
        wx.hideLoading();
        if (res.data.status == 1) {
          //重新赋值后台请求数据
          that.setData({
            is_register: res.data.is_register,
            tel: res.data.tel,
            nickname: res.data.nickname,
            head: res.data.head,
            tipsContent: res.data.tipsContent
          });
        } else if (res.data.status == 100) {
          //自己点自己分享的连接
          wx.redirectTo({
            url: '/pages/my/familyManage'
          });
        } else {
          app.showTip(res.data.msg);
          setTimeout(function() {
            wx.switchTab({
              url: '/pages/index/index'
            });
          }, 2000);
        }
      },
      fail: function() {
        wx.hideLoading();
        app.showTip('抱歉,服务器繁忙');
        setTimeout(function() {
          wx.switchTab({
            url: '/pages/index/index'
          });
        }, 2000);
      }
    });
  },

  //获取微信分享信息
  getShareInfo() {
    var that = this;
    wx.request({
      url: that.data.baseUrl + 'getShareInfo.php',
      method: 'POST',
      data: {

      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function(res) {
        that.setData({
          shareTitle: res.data.shareTitle,
          shareImg: res.data.shareImg
        });
      },
      fail: function(res) {}
    });
  },

  //加入家庭或拒绝加入
  addFamily(e) {
    var that = this;
    var type = e.currentTarget.dataset.type;
    //判断此用户是否注册过了
    if (that.data.is_register == 0) {
      //未注册过 跳转注册
      wx.showModal({
        title: '温馨提示',
        content: that.data.tipsContent,
        complete: (res) => {
          if (res.confirm) {
            //跳转注册界面
            wx.setStorageSync('loginHeaderUrl', '/pages/my/familyInvite?familyId=' + that.data.familyId + "&shareOpenid=" + that.data.shareOpenid + "&billId=" + that.data.billId);
            wx.setStorageSync('loginHeaderType', 'redirectTo');
            if (res.confirm) {
              //跳转注册界面
              wx.redirectTo({
                url: '/pages/login/select-login/select-login'
              });
            }
          }
        }
      });
      return;
    }

    //判断是否填写了身份证号码
    if (that.data.cardNum.trim() != '' && that.data.cardNum.trim().length != 18) {
      app.showTip('请填写18位数的身份证号码');
      return;
    }

    //后端请求
    wx.showLoading({
      title: '申请提交中...'
    })
    wx.request({
      url: that.data.baseUrl + 'addMyFamily.php',
      method: 'POST',
      data: {
        'openid': that.data.openid,
        'familyId': that.data.familyId,
        'shareOpenid': that.data.shareOpenid,
        'billId': that.data.billId,
        'type': type,
        'pinCodes': that.data.cardNum
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function(res) {
        wx.hideLoading();
        app.showTip(res.data.msg);
        if (res.data.status == 1) {
          //重新赋值后台请求数据
          wx.switchTab({
            url: '/pages/my/index'
          });
        }
      },
      fail: function() {
        wx.hideLoading();
        app.showTip('抱歉,服务器繁忙');
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    var that = this;
    return {
      title: that.data.shareTitle,
      path: '/pages/index/index',
      imageUrl: that.data.shareImg,
      success: function(res) {

      },
      fail: function(res) {

      }
    }
  }
})